import { CopyControllerBase } from "./CopyControllerBase";
import { LevelSealInfoPanel } from "../levelSeal/LevelSealInfoPanel";
import { LevelSealRewardsPanel } from "../levelSeal/LevelSealRewardsPanel";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SSeqPlayerItem } from "../../../../../Message/Public/SSeqPlayerItem";
import { SDirectionCopyBossList } from "../../../../../Message/Public/SDirectionCopyBossList";
import { DataEvent } from "../../../events/DataEvent";
import { MapFileUtil } from "../../../scene/map/MapFileUtil";
import { Language } from "../../../../../extend/language/Language";
import { MsgManager } from "../../../manager/MsgManager";
import { LevelSealInfo } from "../../mainUI/info/LevelSealInfo";
import { Game } from "../../../Game";
import { _isShow } from "../../../../../this/_isShow";
//class LevelSealController
    
    export  class LevelSealController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        public get infoPanel(): LevelSealInfoPanel
        {
            if (this._infoPanel == null) 
            {
                this._infoPanel = new LevelSealInfoPanel();
            }
            return this._infoPanel;
        }

        public get rewardsPanel(): LevelSealRewardsPanel
        {
            if (this._rewardPanel == null) 
            {
                this._rewardPanel = new LevelSealRewardsPanel();
            }
            return this._rewardPanel;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCopySealRewardItems, this.onCopySealRewardItemsUpdateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCopySealBossList, this.onCopySealBossListUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.FirstReachLevelSealCommand, this.onFirstReachLevelSealCommandHandler);
            return;
        }

        protected /* override */ addListener(): void
        {
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.addEventListener(EventName.CopyLeaveSealCopy, this.onLeaveSealCopyHandler);
            Dispatcher.addEventListener(EventName.CopyLevelSealGetItemSuccess, this.onGetSealItemSuccessHandler);
            return;
        }

        protected /* override */ removeListener(): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.removeEventListener(EventName.CopyLeaveSealCopy, this.onLeaveSealCopyHandler);
            Dispatcher.removeEventListener(EventName.CopyLevelSealGetItemSuccess, this.onGetSealItemSuccessHandler);
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            this.infoPanel.show();
            this.infoPanel.title = arg1.name;
            this.updateInfoPanel();
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            if (this._infoPanel != null) 
            {
                this._infoPanel.hide();
                this._infoPanel.dispose();
                this._infoPanel = null;
            }
            if (this._rewardPanel != null) 
            {
                this._rewardPanel.hide();
                this._rewardPanel.dispose();
                this._rewardPanel = null;
            }
            cache.copy.levelSeal.bossInfo = new Map<any, any> /* flash.utils.Dictionary */();
            cache.copy.levelSeal.rewards = [];
            return;
        }

        /* internal  */onCopySealRewardItemsUpdateHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSeqPlayerItem;
            cache.copy.levelSeal.rewards = loc1.playerItem;
            if (this._infoPanel) 
            {
                this._infoPanel.updateBonus(loc1.playerItem);
            }
            return;
        }

        /* internal  */onCopySealBossListUpdateHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDirectionCopyBossList;
            cache.copy.levelSeal.bossInfo = loc1.bossInfo;
            if (this._infoPanel) 
            {
                this._infoPanel.updateCondition(loc1.bossInfo);
            }
            if (cache.copy.levelSeal.isOver && cache.copy.levelSeal.hasRewards) 
            {
                this.doLeaveSealCopy(true);
            }
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            if (!cache.copy.isInLevelSealCopy()) 
            {
                return;
            }
            this.updateInfoPanel();
            return;
        }

        /* internal  */updateInfoPanel(): void
        {
            var loc1=1;
            var loc2="";
            var loc3="";
            var loc4="";
            var loc5;
            if ((loc5 = MapFileUtil.mapID % 10) == 1 || MapFileUtil.mapID == 701995 || MapFileUtil.mapID == 702016) 
            {
                loc1 = 80174;
                loc2 = Language.getString(80191);
                loc3 = Language.getString(80186);
                loc4 = Language.getString(80181);
            }
            else if (loc5 == 0 || MapFileUtil.mapID == 701996 || MapFileUtil.mapID == 702019) 
            {
                loc1 = 80175;
                loc2 = Language.getString(80192);
                loc3 = Language.getString(80179);
                loc4 = Language.getString(80182);
            }
            else if (loc5 == 2 || MapFileUtil.mapID == 701997 || MapFileUtil.mapID == 702020) 
            {
                loc1 = 80176;
                loc2 = Language.getString(80193);
                loc3 = Language.getString(80179);
                loc4 = Language.getString(80183);
            }
            else if (loc5 == 3 || MapFileUtil.mapID == 701998 || MapFileUtil.mapID == 702017) 
            {
                loc1 = 80177;
                loc2 = Language.getString(80194);
                loc3 = Language.getString(80179);
                loc4 = Language.getString(80184);
            }
            else if (loc5 == 4 || MapFileUtil.mapID == 701999 || MapFileUtil.mapID == 702018) 
            {
                loc1 = 80178;
                loc2 = Language.getString(80195);
                loc3 = Language.getString(80179);
                loc4 = Language.getString(80185);
            }
            if (this._infoPanel) 
            {
                this._infoPanel.updateView(loc2, loc3, loc4);
                if (loc5 != 1) 
                {
                    this._infoPanel.updateCondition(cache.copy.levelSeal.bossInfo);
                    this._infoPanel.updateBonus(cache.copy.levelSeal.rewards);
                }
                else 
                {
                    this._infoPanel.updateCondition(null);
                    this._infoPanel.updateBonus(null);
                }
            }
            MsgManager.showTaskTarget(Language.getString(loc1));
            return;
        }

        /* internal  */onLeaveSealCopyHandler(arg1: DataEvent): void
        {
            this.doLeaveSealCopy(cache.copy.levelSeal.isOver);
            return;
        }

        /* internal  */doLeaveSealCopy(arg1: Boolean): void
        {
            if (arg1) 
            {
                this.rewardsPanel.setTips(Language.getString(60468));
            }
            else 
            {
                this.rewardsPanel.setTips(Language.getString(60467));
            }
            this.rewardsPanel.setRewards(cache.copy.levelSeal.rewards);
            this.rewardsPanel.show();
            return;
        }

        /* internal  */onGetSealItemSuccessHandler(arg1: DataEvent): void
        {
            if (this._infoPanel) 
            {
                this._infoPanel.hide();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveWithoutComfirm));
            return;
        }

        /* internal  */onFirstReachLevelSealCommandHandler(arg1: LevelSealInfo): void
        {
            if (arg1 == null) 
            {
                return;
            }
            var loc1=false;
            if (arg1.needDays > 0 && arg1.level <= 110) 
            {
                loc1 = true;
            }
            this.showOrHideLevelSealNpc(loc1);
            return;
        }

        /* internal  */showOrHideLevelSealNpc(arg1: Boolean): void
        {
            this._isShow = arg1;
            if (Game.isSceneInit()) 
            {
                this.onSceneUpdate(null);
            }
            else 
            {
                Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            }
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Game.scene.updateNpcIsAddToStage(3000080, this._isShow, 400101);
            if (400101 == MapFileUtil.mapID) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MapUINPCShowOrHide, {"npcId": 3000080, "show": _isShow}));
            }
            return;
        }

        /* internal */ /* var */_infoPanel: LevelSealInfoPanel;

        /* internal */ /* var */_rewardPanel: LevelSealRewardsPanel;

        /* internal */ /* var */_isShow: Boolean=false;
    }
